<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class RecommendController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $name = $request->input('name');
        $list = DB::table('recommend')
            ->join('product', 'product.id', '=', 'recommend.product_id')
            ->select('recommend.*', 'product.name', 'product.keywords', 'product.picture', 'product.lower_price')
            ->where('product.name', 'like', "%{$name}%")
            ->paginate(2);
        return view('admin.recommend', ['list'=>$list]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //
        // $pro = DB::table('product')->get();
        $where = [];
        $db = DB::table('product');
        if($request->has('name')){
            // 获取要搜索的title字段的值
            $name = $request->input('name');
            //添加where模糊匹配条件
            $db->where('name','like',"%{$name}%");
            $where['name'] = $name;
        }
        $pro = $db->paginate(2);
        return view('admin.addrec', ['pro'=>$pro, 'where'=>$where]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // dd($request);
        $id = $request->input('product_id');
        $rec = DB::table('recommend')->where('product_id', $id)->get()->count();
        if($rec < 1){
            // 插入商品推荐表
            $data = $request->only('product_id');
            $m = DB::table('recommend')->insertGetId($data);
            if($m>0){
                return redirect('admin/recommend');
            }
        }else{
            return redirect('admin/recommend');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $row = DB::table('recommend')->where('id',$id)->delete();
        if($row>0){
            return redirect('admin/recommend');
        }
    }
}
